package com.atwisdom.star.service.status;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.util.SpringContextUtil;
import com.atwisdom.star.core.dsl.modelClass.resource.Impl.ResourceInstanceDslServiceImpl;

/**
 * @ClassName: EquipmentStatus
 * @ Description:设备状态管理
 * @author:王红艳
 * @date:2024/05/31 16:12
 */
public class EquipmentStatus extends StatusMachine{
    private final ResourceInstanceDslServiceImpl resourceInstanceDslServiceImpl  = SpringContextUtil.getBean(ResourceInstanceDslServiceImpl.class);

    public EquipmentStatus(Long id){
        this.setId(id);
        this.initStatus();
        this.setStatus();
    }
    /**
     * 初始化状态
     */
    public void initStatus(){
        super.setStatusMap("创建","启用")
                .setStatusMap("启用","停用")
                .setStatusMap("停用","启用")
                .setStatusMap("启用","废弃")
                .setStatusMap("停用","废弃");
        super.setRunStatusMap("开机","停机")
                .setRunStatusMap("停机","开机")
                .setRunStatusMap("开机","故障停机")
                .setRunStatusMap("停机","故障停机")
                .setRunStatusMap("故障停机","开机");
    }

    /**
     * 更新状态
     */
    @Override
    public void updateStatus() {
        JSONObject jsonObject = JsonUtil.buildJson(new Object[]{"id",this.getId(),"values",JsonUtil.buildJson(new Object[]{this.getUpdateKey(),this.getUpdateStatus()})});
        resourceInstanceDslServiceImpl.updateById(jsonObject);
    }

    /**
     * 初始化状态
     */
    @Override
    public void setStatus(){
        JSONObject jsonObject = resourceInstanceDslServiceImpl.queryById(this.getId());
        this.setStatus(JsonUtil.get(jsonObject,"values.status"));
        this.setRunStatus(JsonUtil.get(jsonObject,"values.runStatus"));
    }

    /*测试 更新设备运行状态，前提设备状态为启动
    EquipmentStatus equipmentStatus = new EquipmentStatus(id);
        equipmentStatus.setUpdateStatus(equipmentStatus.getRunStatusKey(),status);
        equipmentStatus.next(equipmentStatus.checkStatus("启动"));*/
}
